<?php

namespace app\admin\controller;

//use think\Controller;
use think\Request;
use think\Db;
use think\Session;

//专辑列表
class Specials extends Admin
{

    public function __construct(){
        parent::initialize();
        // var_dump($this->initialize()) ;

    }
    public function specials()
    {	
    	$list = Db::name('tb_specials')->paginate(3);
    	// 把分页数据赋值给模板变量list
	
        $data = Db::name('tb_special_classify')->where('parent_id',0)->select();
        foreach ($data as $k => $v) {
            $res = Db::name('tb_special_classify')->where('parent_id',$v['id'])->select();
            foreach ($res as $a => $b) {
               $result[$v['name']][] = $b['name'];
            }
        }     
        // var_dump($result);   
        return $this->fetch('specials/specials',[
                'list'=>$list,
                'res' =>$result

            ]);
    }

    //添加时查找歌手
    public function singer(Request $request)
    {   
        $data = Request::instance()->post();

        $list = Db::name('tb_singers')->where('name',$data['singer'])->value('id');
        echo json_encode($list);

    }

    // 添加专辑
    public function add(Request $request)
    {   

        $file = request()->file('image');
        $data = Request::instance()->post();
        // var_dump($file,$data);
        if ($file){

            $info = $file->validate(['size'=>156780000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
            $list = Db::name('tb_singers')->where('name',$data['singer'])->value('id');
        }
        if(!empty($info)){
            // 成功上传后 获取上传信息       
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
             $img = $info->getSaveName();
            $imgs = str_replace('\\', '/', $img);
            if (!empty($list)) {

                $result=Db::table('tb_specials')
                    ->data(['name'=>$data['name'],'singerid'=>$list,'dc'=>$data['dsc'],'addtime'=>date('y-m-d H:i:s',time()),'singer'=>$data['singer'],'imgs'=>$imgs,'style'=>$data['style']])
                    ->insert();
                if ($result){

                    return $this->success('添加成功','specials');
                }else{
                    return $this->error('添加失败,有数据未填');

                }

            }else{
                return $this->error('歌手不存在');

            }
            
        }else{
            
            return $this->error('图片不能为空');
     
        }
       
    }

    //删除专辑
    public function del(Request $request)
    {
        $data = Request::instance()->post();
        $list = Db::name('tb_specials')->where('id',$data['id'])->delete();
        echo json_encode($list);
        
    }

//显示编辑专辑页面
    public function edit($id)
    {   
        $ddd = Db::name('tb_special_classify')->where('parent_id',0)->select();
        foreach ($ddd as $k => $v) {
            $res = Db::name('tb_special_classify')->where('parent_id',$v['id'])->select();
            foreach ($res as $a => $b) {
               $result[$v['name']][] = $b['name'];
            }
        }   


        $list = Db::name('tb_specials')->where('id',$id)->select();
        $data = $list[0];
//        var_dump($list);die;

        return $this->fetch('addspecials/addspecials',[
                        'data'=>$data,
                        'res'=>$result


            ]);
    }

//编辑专辑


    public function update(Request $request)
    {


        $data = Request::instance()->post();
        $list = Db::name('tb_singers')->where('name',$data['singer'])->value('id');
        if ($list){

            $data=Db::table('tb_specials')
                ->where('id', $data['id'])
                ->update(['name'=>$data['name'],'singerid'=>$list,'dc'=>$data['dsc'],'singer'=>$data['singer'],'vip'=>$data['vip'],'style'=>$data['style']]);

            if ($data){
                return $this->success('更新成功','specials');
            } else{
                return  $this->error('更新失败');
            }

        } else {
           return $this->error('更新失败');
        }

    }


    public function updateimg(Request $request)
    {   

        $data = Request::instance()->post();
        $file = request()->file('image');
        if ($file){
            $info = $file->validate(['size'=>1500000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
         // var_dump($file);die;

            if (!empty($info)) {
            // 成功上传后 获取上传信息                       
            $img = $info->getSaveName();
            }

            $imgs = str_replace('\\', '/', $img);
            $list=Db::table('tb_specials')
                    ->where('id', $data['id'])
                    ->update(['imgs'=>$imgs]);
            if($list){
                return $this->success('更新图片成功','specials');
            } else {
                return $this->error('更新图片失败');
            }

        }else{
            return $this->error('更新图片失败');
        }

    }

}



